package com.example.myapplication.ui.mine

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.myapplication.R
import com.example.myapplication.data_class.order_movie
import okhttp3.OkHttpClient
import okhttp3.Request
import org.json.JSONArray
import java.lang.Exception
import java.lang.StringBuilder
import kotlin.concurrent.thread

class WangToWatchActivity : AppCompatActivity() {

    private var mRecyclerView: RecyclerView? = null
    private var mAdapter: WantInfoAdapter? = null
    val wantInfoList = ArrayList<WantInfo>()
    val sever_ip = "192.168.43.8"
    val showAll_WantedMoive_jsp = "http://${sever_ip}:8080/JspForAndroid/showAll_WantedMovie.jsp"
    //val showAll_WantedMoive_jsp = "http://${R.string.sever_ip}:8080/JspForAndroid/showAll_WantedMovie.jsp"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_wang_to_watch)

        val layoutManager = LinearLayoutManager(this)
//        myrecyclerview_orider.layoutManager = layoutManager
//        val adapter2 =OrderlistAapter(order_list)
//        myrecyclerview_orider.adapter = adapter2
        mRecyclerView = findViewById(R.id.myrecyclerview_want)
        mRecyclerView?.layoutManager = layoutManager
        mAdapter = WantInfoAdapter(wantInfoList, applicationContext)
        mRecyclerView?.adapter = mAdapter

        initData()
    }

    private fun initData(){
        //wantInfoList.add(WantInfo("2131165335"))
        //wantInfoList.add(WantInfo("2131165335"))
        getAllTrainInfoJson()
    }

    private fun getAllTrainInfoJson(){
        thread {
            try {
                val client = OkHttpClient()

                //构建请求
                val request = Request.Builder()
                    .url(showAll_WantedMoive_jsp)
                    .build()
                //执行
                val response= client.newCall(request).execute()
                //得到返回值
                val responseData = response.body?.string()
                //txtData.text = responseData
                val result = parseJson(responseData)
                if (responseData != null){
                    runOnUiThread{
                        //修改布局
                        //txtData.text = result
//                        val layoutManager = LinearLayoutManager(this)
//                        myrecyclerview_orider.layoutManager = layoutManager
//                        val adapter2 =OrderlistAapter(order_list)
//                        myrecyclerview_orider.adapter = adapter2
                        mAdapter?.notifyDataSetChanged()
                        mRecyclerView?.adapter = mAdapter
                    }
                }
            }catch (e: Exception){
                e.printStackTrace()
            }
        }
    }
    private fun parseJson(jsonData:String?):String{
        val builder = StringBuilder()
        try {
            //json数组

            val jsonArray = JSONArray(jsonData)
            //清空原来的子项
            wantInfoList.clear()
            //向列表中加入子项
            for (i in 0 until jsonArray.length()){
                //取出元素
                val jsonObject = jsonArray.getJSONObject(i)
                val want_movie_src = jsonObject.getString("wanted_movie_src")

                //builder.append("$movie_name--$movie_src--$movie_score--$movie_kind, ")
                wantInfoList.add(WantInfo(want_movie_src))

            }
        }catch (e: Exception){
            e.printStackTrace()
        }

        return builder.toString()
    }
}